package com.fighYo.trace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Scan;
import com.fighYo.trace.service.ScanService;
import com.fighYo.trace.utils.R;
import com.fighYo.trace.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-17
 */
@Api(tags = "溯源码扫描情况管理")
@CrossOrigin
@RestController
@RequestMapping("/scan")
public class ScanController {

    @Autowired
    private ScanService scanService;

    @ApiOperation("总数")
    @GetMapping("count")
    private R count() {
        int count = scanService.count(null);
        return R.ok().data("item", count);
    }

    public Integer getCount(String result, String startTime, String endTime) {
        QueryWrapper<Scan> wrapper = new QueryWrapper<>();
        wrapper.ge("happen_time", startTime);
        wrapper.le("happen_time", endTime);
        if (!StringUtils.isEmpty(result)) {
            wrapper.eq("result", result);
        }
        return scanService.count(wrapper);
    }

    @ApiOperation("指定天数内扫描情况")
    @GetMapping("info")
    public R info(String dayNum) {

        Integer total = Integer.valueOf(dayNum) - 1;

        List<String> dateList = new ArrayList<>();
        List<Integer> count0List = new ArrayList<>();
        List<Integer> count1List = new ArrayList<>();
        List<Integer> count2List = new ArrayList<>();
        List<Integer> totalList = new ArrayList<>();
        for (int i = total; i >= 0; i--) {

            Date begin = TimeUtil.getDayBegin();
            Date end = TimeUtil.getDayEnd();
            String startTime = TimeUtil.dateFormat(TimeUtil.getFrontDay(begin, i));
            String endTime = TimeUtil.dateFormat(TimeUtil.getFrontDay(end, i));
            dateList.add(startTime.split(" ")[0]);
            count0List.add(getCount("0", startTime, endTime));
            count1List.add(getCount("1", startTime, endTime));
            count2List.add(getCount("2", startTime, endTime));
            totalList.add(getCount(null, startTime, endTime));
        }
        return R.ok().data("dateList", dateList)
                .data("count0List", count0List)
                .data("count1List", count1List)
                .data("count2List", count2List)
                .data("totalList", totalList);

    }

}

